package com.liy.framework.common.openoffice;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OpenOfficeUtil {
    protected static Logger log = LoggerFactory.getLogger(OpenOfficeUtil.class);
    private String ip = "127.0.0.1";
    private int port = 8100;

    public OpenOfficeUtil(String openofficeHome) {
        log.info("office: " + openofficeHome);
        if (openofficeHome.indexOf(":") > 0) {
            this.ip = openofficeHome.split(":")[0];
            this.port = Integer.parseInt(openofficeHome.split(":")[1]);
        }

    }

    public boolean convert2Pdf(String inputFilePath, String outputFilePath) throws Exception {
        long s = System.currentTimeMillis();
        log.info("start convert office inputFilePath: " + inputFilePath);
        File file = new File(outputFilePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
            log.info("File dir not exist, create dir: " + file.getParentFile().getAbsolutePath());
        }

        OpenOfficeTool.convert(inputFilePath, outputFilePath, this.ip, this.port);
        log.info("convert office time: " + (System.currentTimeMillis() - s));
        return true;
    }
}
